/******************************************************************************************************
Copyright (C) 2010 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

//! Multiplet Changed Event, the argument will define the itemId and the multipletId
function onMultipletChanged(aJSONString)
{
	var info = JSON.parse(aJSONString); //this constructs the argument object parsing the JSON string sent as parameter
	MessageBox.information("Multiplet changed: "+aJSONString, MessageBox.Ok)
	return true;
}
Application.mainWindow.installEventHandler("nmrMultipletChanged", "onMultipletChanged")

//! Multiplet Selected Event, the argument will define the itemId and the multipletId
function onMultipletClicked(aJSONString)
{
	var info = JSON.parse(aJSONString);//this constructs the argument object parsing the JSON string sent as parameter
	print("Multiplet Clicked: "+aJSONString)
	return true;
}
Application.mainWindow.installEventHandler("nmrMultipletClicked","onMultipletClicked")

//! Multiplet Highlighted Event, the argument will define the itemId and the multipletId
function onMultipletHighlighted(aJSONString)
{
	var info = JSON.parse(aJSONString);//this constructs the argument object parsing the JSON string sent as parameter
	print("Multiplet Highlighted: "+JSON.stringify(aInfo))
	return true;
}
//Application.mainWindow.installEventHandler("nmrMultipletHighlighted","onMultipletHighlighted")


//! Integral Changed Event, the argument will define the itemId and the integralId
function onIntegralChanged(aJSONString)
{
	var info = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	MessageBox.information("Integral changed: "+aJSONString, MessageBox.Ok)
	return true;
}
Application.mainWindow.installEventHandler("nmrIntegralChanged", "onIntegralChanged")

//! Assignment, the argument will define the spectrumId, the moleculeId, the atomNumber, the shiftMax and shiftMin
function onAssignment(aJSONString)
{
	var info = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	MessageBox.information("onAssignment: "+aJSONString, MessageBox.Ok)
	return true;
}
Application.mainWindow.installEventHandler("nmrAssignmentMade", "onAssignment")

//! Assignment Highlighted, the argument will define the spectrumId, the moleculeId, the atomNumber, the shiftMax and shiftMin
function onAssignmentHighLighted(aJSONString)
{
	var info = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	print( "HIGHLIGHT: "+aJSONString)
	return true;
}
//Application.mainWindow.installEventHandler("nmrAssignmentHighlighted", "onAssignmentHighLighted")

//! NMR Spectrum Imported Event, the argument contains the itemId
function onNMRSpectrumImported(aJSONString)
{
	print("onNMRSpectrumImported "+aJSONString)
	var info = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	MessageBox.information(aJSONString, MessageBox.Ok)
	return true;
}
Application.mainWindow.installEventHandler("nmrSpectrumImported", "onNMRSpectrumImported")

//! DB Items Pasted Event, the argument is an array with the itemIds of the pasted items
function onDBItemsPasted(aJSONString)
{
	print("onDBItemsPasted "+aJSONString)
	var info = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	//MessageBox.information(aJSONString, MessageBox.Ok)
	var msg = "Pasted " + info.length + " items:";
	for (var i = 0; i < info.length; i++) msg += "\n  Id=" + info[i];
	MessageBox.information(msg, MessageBox.Ok)
	return true;
}
Application.mainWindow.installEventHandler("dbItemsPasted", "onDBItemsPasted")

/** Example function to handle mouse button double click events. Return false to let Mnova handle the event. Mnova will call this function with one argument with these properties: 
	- itemId:String. Acctive item id, might be undefined
	- x:Number. X coordinate in canvas units
	- y:Number. Y coordinate in canvas units 
	- button:String. The button pressed: "LeftButton","RightButton" or undefined
*/
function onMouseButtonDoubleClick(aJSONString)
{
	var aArgument = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	var actDoc = mainWindow.activeDocument
	if(aArgument.itemId!==undefined) {
		var item = actDoc.getItem(aArgument.itemId);
		if(item!=undefined) {
			if(item.name=="NMR Spectrum" && aArgument.button=="LeftButton") {
				var nmrItem = new NMRSpectrum(item);
				MessageBox.information(nmrItem.toString()+"\nSpectrum Index: "+nmrItem.curSpecIndex+"\nPos: "+aArgument.x+" "+aArgument.y, MessageBox.Ok)
				return true;
			}
		}
	}
	return false;
}
Application.mainWindow.installEventHandler("MouseButtonDblClick", "onMouseButtonDoubleClick")

/** Example function to handle mouse move events. Return false to let Mnova handle the event. Mnova will call this function with one argument with these properties: 
	- itemId:String. Acctive item id, might be undefined
	- x:Number. X coordinate in canvas units
	- y:Number. Y coordinate in canvas units 
*/
function onMouseMove(aJSONString)
{
	var aArgument = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	if(aArgument.button=="LeftButton")
		print("Mouse Move: ", aArgument.x, aArgument.y);
	return false;
}
//Application.mainWindow.installEventHandler("MouseMove", "onMouseMove")

/** Example function to handle key press events. Return false to let Mnova handle the event. Mnova will call this function with one argument with these properties: 
	- itemId:String. Acctive item id, might be undefined
	- key:String. Key pressed with modificators, e.g. "C", "Ctrl+G", "Shift+U", "Alt+I"
*/
function onKeyPress(aJSONString)
{
	var aArgument = JSON.parse(aJSONString)//this constructs the argument object parsing the JSON string sent as parameter
	var actDoc = mainWindow.activeDocument
	if(aArgument.itemId!==undefined) {
		var item = actDoc.getItem(aArgument.itemId);
		if(item!=undefined) {
			if(item.name=="NMR Spectrum" && aArgument.key!="Esc") {
				var nmrItem = new NMRSpectrum(item);
				MessageBox.information(nmrItem.toString()+"\nKey: "+aArgument.key, MessageBox.Ok)
				return true;
			}
		}
	}
	return false;
}
Application.mainWindow.installEventHandler("KeyPress", "onKeyPress")

/** Example function to handle selectionChanged event. Return true to avoid propagation of the event. If the selection is empty aJSONString will be empty. Mnova will call this function with one argument with these properties: 
	- activeId:String. Acctive item id.
	- selectionIds: Array. Ids of all the item selected.
Besides this properties, an array with the item ids of the same type will be added under a property named with the item name ("NMR Spectrum", "Molecule", ...)
*/
function onSelectionChanged(aJSONString) {
	'use strict';
	if (aJSONString) {
		try {
			var value = JSON.parse(aJSONString);
			print(JSON.stringify(value));
			if (value['NMR Spectrum']) {
				print("Selection has NMR");
			}
		} catch (er) {
			print(er);
		}
	} else {
		print("Empty Selection");
	}
}
Application.mainWindow.installEventHandler("selectionChanged", "onSelectionChanged");


